//#pragma code

/******************************************************************************
 Project:    Buzzer
 Modul:      main.c
 Descr:      hier gehts los...

 Date:       25.07.10
 Autor:      ge, rk
 Notes:      -
 ******************************************************************************/

#include "main.h"
#include "setup.h"
#include "com.h"
#include "port.h"

#include "buzzer_game.h"

#include "summer.h"
#include "timer.h"

#include <avr/wdt.h >     /*Watchdog*/

volatile uc ucExit;
volatile uc ucWatchdog;
volatile uc ucReset;
volatile uc ucBlinkTimeTest;

int main(void)
{
   ucReset = FALSE;

   while (1)
   {
      //ini #####################################################################
      SETUP_vIniProzessor();

      /*Enable Watchdog */
      //wdt_enable(WDTO_250MS);
      /*bei 5V .... bei 3 V sind die Zeiten etwa 3x hoeher
       WDTO_15MS      0
       WDTO_30MS      1
       WDTO_60MS      2
       WDTO_120MS     3
       WDTO_250MS     4
       WDTO_500MS     5
       WDTO_1S        6
       WDTO_2S        7     */

      //Com ini -----------------------------------------------------------------
      COM_vIniCOM();

      //Ports ini----------------------------------------------------------------
      PORT_vIniPort();

      //Timer ini ---------------------------------------------------------------
      TIMER_vIniTimer();

      //BUZZER initialisieren -----------------------------------------------------
      BUZZER_GAME_vIni();

      //Test-Test-Test-Test
      ucBlinkTimeTest = 1;

      sei();
      //global Interrupt einschalten

      //-------------------------------------------------------------------------
      ucExit = 0;

      //Test - kind of reset information
      //COM_vTxByte(0xF1);
      COM_vTxByte('H');
      COM_vTxByte('A');
      COM_vTxByte('L');
      COM_vTxByte('L');
      COM_vTxByte('O');

      TIMER_vSetTimer(TIMER_COM_TX_TEST, 2000);

      while (ucExit == 0)
      {
         //Check Application #####################################################

         //Watchdog ##############################################################
         //if (ucWatchdog == TRUE)
         {
         //   ucWatchdog = FALSE;
            //watchdog();
         }

         //just for test reason ----------------------------
         if (TIMER_ucAskTimer(TIMER_COM_TX_TEST) == TRUE)
         {
            TIMER_vSetTimer(TIMER_COM_TX_TEST, 2000);
            COM_vTxByte('X');
         }
         //-------------------------------------------------

         if (TIMER_ucAskTimer(TIMER_GAME_PLAYER_TIMEOUT) == TRUE)
         {
            BUZZER_GAME_vPlayerTimeout();
         }
//         if (TIMER_ucAskTimer(TIMER_GAME_ROUND_TIMEOUT) == TRUE)
//         {
//            BUZZER_GAME_vRoundTimeout();
//         }

         //Check HighLevel Treiber ###############################################
         //Summer ----------------------------------------------------------------
         //SUMMER_vSummer();

         //Check LowLevel Treiber ################################################
         //COM schreiben ---------------------------------------------------------
         //--> wird im Interrupt gemacht...

      } //end while(ucExit)

      //global interrupt ausschalten
      cli();

      ucReset = TRUE;

   } //end while(1)
} //end main

void watchdog(void)
{
   /*Watchdog aufziehen*/
   //wdt_reset();
} // watchdog
